package com.eyas.business.service.appointmentinfo;

import com.eyas.business.config.webmvc.exception.custom.uncheck.EyasBusinessException;
import com.eyas.business.config.webmvc.interceptor.LoginInterceptor;
import com.eyas.business.model.jdbcEntity.table.AppointmentInfo;
import com.eyas.business.model.jdbcEntity.table.ChildrenInfo;
import com.eyas.business.model.jdbcEntity.table.RecordInfo;
import com.eyas.business.model.jdbcEntity.table.UserInfo;
import com.eyas.business.model.jpa.AdminInfo;
import com.eyas.business.model.pojo.appointmentinfo.QueryParamDTO;
import com.eyas.business.model.pojo.appointmentinfo.QueryResultDTO;
import com.eyas.business.model.pojo.appointmentinfo.YKParamDTO;
import com.eyas.business.model.pojo.webchat.AppointMentInfoDTO;
import com.eyas.core.model.page.PageResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpSession;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.eyas.core.jdbc.utils.CommonJdbcUtils.commonJdbcUtils;

/**
 * @Auther: 王龙龙
 * @Date: 2019/3/16 17:17
 * @Description:
 */
@Service
@Transactional
public class AppointmentInfoServiceImpl implements AppointmentInfoService {
    @Override
    public PageResult<QueryResultDTO> queryAppointmentInfo(QueryParamDTO dto, int page, int limit) {
        StringBuffer sb = new StringBuffer("");
        sb.append(" select a.*,e.username as operationname,c.classid,d.coachid,b.starttime,b.begintime,b.endtime,b.address,g.cname, ")
                .append(" c.classname,d.coachname,(select keyworlds from keyworldsinfo where dcode='ISDY' and keycode=a.isdy ) as isdy_str, ")
                .append(" (case when isdy='1' then '会员本人' else (select username from admininfo where adminnid=a.appuserid) end) as appusername, ")
                .append(" (select keyworlds from keyworldsinfo where dcode='YKSTATUS' and keycode=a.status ) as status_str ")
                .append(" from appointmentinfo a left join admininfo e on a.operationid=e.adminnid ")
                .append(" ,recordinfo b,classinfo c,coachinfo d,recordandcoach f,childreninfo g ")
                .append(" where a.recordid=b.recordid and b.classid=c.classid and b.recordid=f.recordid and f.coachid=d.coachid ")
                .append(" and f.coachflag='1' and a.cid=g.cid ")
                .append(" and a.userid=? order by atime desc ");
        return commonJdbcUtils.queryForPage(sb.toString(),QueryResultDTO.class,page,limit,dto.getUserid());
    }

    private Lock recordinfo_lock = new ReentrantLock();
    @Override
    public void saveAppointmentInfo(YKParamDTO ykParamDTO, HttpSession session) {
        RecordInfo recordInfo = commonJdbcUtils.queryObject("select * from recordinfo where recordid=?",RecordInfo.class,ykParamDTO.getRecordid());
        SimpleDateFormat simpleDateFormat1 =  new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 =  new SimpleDateFormat("HH:mm:ss");
        Date current = new Date();
        //判断预约课程是否在7天之内
       /* double minus = (recordInfo.getStarttime().getTime() - current.getTime())/1000/60/60/24;
        if(minus>7||minus<0)
            throw new EyasBusinessException("只能预约未来一周内的课程");*/

        for(int i=0;i<ykParamDTO.getChildrens().length;i++) {
            ChildrenInfo childrenInfo = commonJdbcUtils.queryObject("select * from childreninfo where cid=?",ChildrenInfo.class,ykParamDTO.getChildrens()[i]);
            Date birthday=childrenInfo.getBirthday();
            if(birthday==null)
                birthday=new Date();
            long day=(current.getTime()-birthday.getTime())/(24*60*60*1000) + 1;
            String year=new java.text.DecimalFormat("#.00").format(day/365f);
            BigDecimal age = new BigDecimal(year);

            String tipsString="预约失败，您孩子的年龄不适合参加该课程";
            if (recordInfo.getAgerange().equals("1")) {
                if(age.compareTo(new BigDecimal("2"))>0&&age.compareTo(new BigDecimal("3.5"))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("2")) {
                if(age.compareTo(new BigDecimal("2.5"))>0&&age.compareTo(new BigDecimal("4.5"))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("3")) {
                if(age.compareTo(new BigDecimal(3.5+""))>0&&age.compareTo(new BigDecimal(5.5+""))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("4")) {
                if(age.compareTo(new BigDecimal(4.5+""))>0&&age.compareTo(new BigDecimal(6.5+""))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("5")) {
                if(age.compareTo(new BigDecimal(5.5+""))>0&&age.compareTo(new BigDecimal(7.5+""))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("6")) {
                if(age.compareTo(new BigDecimal(6.5+""))>0&&age.compareTo(new BigDecimal(9.5+""))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }
            else if (recordInfo.getAgerange().equals("7")) {
                if(age.compareTo(new BigDecimal(2.5+""))>0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("8")) {
                if(age.compareTo(new BigDecimal(3.5+""))>0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("9")) {
                if(age.compareTo(new BigDecimal(4.5+""))>0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("10")) {
                if(age.compareTo(new BigDecimal(5.5+""))>0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }

            //增加校验，同一个课程不能同时存在多个有效的约课记录
            int count = commonJdbcUtils.queryObject("select count(1) from appointmentinfo where cid=? and recordid=? and status<>'3'",Integer.class,ykParamDTO.getChildrens()[i],
                    ykParamDTO.getRecordid());
            if(count>0)
                throw new EyasBusinessException("会员已经预约了该课程,请不要重复预约");

            //同一个时间段的课程不能预约多个
            count = commonJdbcUtils.queryObject("select count(1) from appointmentinfo a,recordinfo b where a.cid=? and a.recordid=b.recordid " +
                            " and a.status<>'3' and b.status<>'4' and b.starttime=? and (( " +
                            " (b.begintime>? and b.begintime<?) " +
                            " or (b.endtime>? and b.endtime<?) " +
                            " or (b.begintime<? and b.endtime>?) ) or b.begintime=? and b.endtime=?) ",Integer.class,ykParamDTO.getChildrens()[i],
                    simpleDateFormat1.format(recordInfo.getStarttime()),
                    simpleDateFormat2.format(recordInfo.getBegintime()),simpleDateFormat2.format(recordInfo.getEndtime()),
                    simpleDateFormat2.format(recordInfo.getBegintime()),simpleDateFormat2.format(recordInfo.getEndtime()),
                    simpleDateFormat2.format(recordInfo.getBegintime()),simpleDateFormat2.format(recordInfo.getEndtime()),
                    simpleDateFormat2.format(recordInfo.getBegintime()),simpleDateFormat2.format(recordInfo.getEndtime()));
            if(count>0)
                throw new EyasBusinessException("在["+simpleDateFormat1.format(recordInfo.getStarttime())+"  "+simpleDateFormat2.format(recordInfo.getBegintime())
                        +"]已经存在有效的预约课程");
        }

        //校验该课程是否已经开课，超过begintime的不能预约
        /*Date starttime = recordInfo.getStarttime();
        Date begintime = recordInfo.getBegintime();
        SimpleDateFormat dayformat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat timeformat = new SimpleDateFormat("HHmmss");
        if(Integer.parseInt(dayformat.format(starttime))<Integer.parseInt(dayformat.format(current))) {
            throw new EyasBusinessException("该课程已经开始上课，不能预约");
        }else if(Integer.parseInt(dayformat.format(starttime))==Integer.parseInt(dayformat.format(current))) {
            if(Integer.parseInt(timeformat.format(begintime))<Integer.parseInt(timeformat.format(current)))
                throw new EyasBusinessException("该课程已经开始上课，不能预约");
        }*/

        try {
            recordinfo_lock.lock();
            if(recordInfo.getStatus().equals("4"))
                throw new EyasBusinessException("这个班级的状态[已取消],无法预约");
            int currentnum = recordInfo.getCurrnum()==null?0:recordInfo.getCurrnum();
            int maxnum = recordInfo.getMaxnum()==null?0:recordInfo.getMaxnum();
            if(currentnum+ykParamDTO.getChildrens().length>maxnum)
                throw new EyasBusinessException("这个班级的剩余上课名额不足，不能再预约");
            recordInfo.setCurrnum(currentnum+ykParamDTO.getChildrens().length);
            commonJdbcUtils.update(recordInfo);
        }finally {
            recordinfo_lock.unlock();
        }


        for(int i=0;i<ykParamDTO.getChildrens().length;i++) {
            AppointmentInfo appointmentInfo = new AppointmentInfo();
            AdminInfo adminInfo = (AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME);
            appointmentInfo.setUserid(ykParamDTO.getUserid());
            appointmentInfo.setRecordid(ykParamDTO.getRecordid());
            appointmentInfo.setCid(ykParamDTO.getChildrens()[i]);
            appointmentInfo.setAtime(new Date());
            appointmentInfo.setIsdy("2"); //代约课
            appointmentInfo.setAppuserid(adminInfo.getAdminnid());//代约经办人
            appointmentInfo.setStatus("1");//未签到
            commonJdbcUtils.insert(appointmentInfo);
        }

    }


    private Lock userinfo_lock = new ReentrantLock();
    @Override
    public void updateAppointmentInfo(QueryParamDTO dto, HttpSession session) {
        AppointmentInfo appointmentInfo = commonJdbcUtils.queryObject("select * from appointmentinfo where appointmentid=?",AppointmentInfo.class,dto.getAppointmentid());
        AdminInfo adminInfo = (AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME);
        if(dto.getStatus().equals("3")) {//取消约课
            appointmentInfo.setStatus("3");
            appointmentInfo.setConsumes(0);
            appointmentInfo.setOperationid(adminInfo.getAdminnid());
            appointmentInfo.setOperationtime(new Date());

            //recordinfo的当前报名人数-1
            commonJdbcUtils.update("update recordinfo set currnum=currnum-1 where recordid=?",appointmentInfo.getRecordid());
        }else {//约课已经签到并扣除会员的课时
            try {
                //加锁，保证并发情况下课时处理不会出问题
                userinfo_lock.lock();
                UserInfo userInfo = commonJdbcUtils.queryObject("select * from userinfo where userid=?",UserInfo.class,appointmentInfo.getUserid());
                if(dto.getConsumes()==0) {
                    userInfo.setRestclass(userInfo.getRestclass()==null?0:userInfo.getRestclass());
                    userInfo.setTiyannums(userInfo.getTiyannums()==null?0:userInfo.getTiyannums());
                }else {
                    int user_rest_class = userInfo.getRestclass()==null?0:userInfo.getRestclass();
                    int user_tiyan_class = userInfo.getTiyannums()==null?0:userInfo.getTiyannums();
                    int totalClass = user_rest_class+user_tiyan_class;
                    if(totalClass==0)
                        throw new EyasBusinessException("会员剩余的课时不足，请及时充值");
                    int restclass = user_rest_class - dto.getConsumes();
                    if(restclass<0) {
                        int resttiyannums = user_tiyan_class + restclass;
                        if(resttiyannums<0)
                            throw new EyasBusinessException("会员剩余的课时不足，请及时充值");
                        userInfo.setTiyannums(resttiyannums);
                        userInfo.setRestclass(0);
                        appointmentInfo.setConsumest(restclass*-1);
                    }else {
                        userInfo.setRestclass(restclass);
                    }

                    commonJdbcUtils.update(userInfo);
                }
            }finally {
                userinfo_lock.unlock();
            }

            appointmentInfo.setStatus("2");//已签到
            appointmentInfo.setConsumes(dto.getConsumes());
            appointmentInfo.setOperationid(adminInfo.getAdminnid());
            appointmentInfo.setOperationtime(new Date());
        }

        commonJdbcUtils.update(appointmentInfo);
    }

    private Lock app_lock = new ReentrantLock();
    @Override
    public void saveAppointmentInfoPublic(YKParamDTO ykParamDTO) {
        RecordInfo recordInfo = commonJdbcUtils.queryObject("select * from recordinfo where recordid=?",RecordInfo.class,ykParamDTO.getRecordid());
        Date current = new Date();
        SimpleDateFormat simpleDateFormat1 =  new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 =  new SimpleDateFormat("HH:mm:ss");
        //判断预约课程是否在7天之内
        double minus = (recordInfo.getStarttime().getTime() - current.getTime())/1000/60/60/24;
        if(minus>7||minus<0)
            throw new EyasBusinessException("只能预约未来一周内的课程");
        for(int i=0;i<ykParamDTO.getChildrens().length;i++) {
            ChildrenInfo childrenInfo = commonJdbcUtils.queryObject("select * from childreninfo where cid=?",ChildrenInfo.class,ykParamDTO.getChildrens()[i]);
            Date birthday=childrenInfo.getBirthday();
            if(birthday==null)
                birthday=new Date();
            long day=(current.getTime()-birthday.getTime())/(24*60*60*1000) + 1;
            String year=new java.text.DecimalFormat("#.00").format(day/365f);
            BigDecimal age = new BigDecimal(year);

            String tipsString="预约失败，您孩子的年龄不适合参加该课程";
            if (recordInfo.getAgerange().equals("1")) {
                if(age.compareTo(new BigDecimal("2"))>0&&age.compareTo(new BigDecimal("3.5"))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("2")) {
                if(age.compareTo(new BigDecimal("2.5"))>0&&age.compareTo(new BigDecimal("4.5"))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("3")) {
                if(age.compareTo(new BigDecimal(3.5+""))>0&&age.compareTo(new BigDecimal(5.5+""))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("4")) {
                if(age.compareTo(new BigDecimal(4.5+""))>0&&age.compareTo(new BigDecimal(6.5+""))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("5")) {
                if(age.compareTo(new BigDecimal(5.5+""))>0&&age.compareTo(new BigDecimal(7.5+""))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("6")) {
                if(age.compareTo(new BigDecimal(6.5+""))>0&&age.compareTo(new BigDecimal(9.5+""))<0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }
            else if (recordInfo.getAgerange().equals("7")) {
                if(age.compareTo(new BigDecimal(2.5+""))>0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("8")) {
                if(age.compareTo(new BigDecimal(3.5+""))>0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("9")) {
                if(age.compareTo(new BigDecimal(4.5+""))>0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }else if (recordInfo.getAgerange().equals("10")) {
                if(age.compareTo(new BigDecimal(5.5+""))>0){
                }else{
                    throw new EyasBusinessException(tipsString);
                }
            }

            //增加校验，同一个课程不能同时存在多个有效的约课记录
            int count = commonJdbcUtils.queryObject("select count(1) from appointmentinfo where cid=? and recordid=? and status<>'3'",Integer.class,ykParamDTO.getChildrens()[i],
                    ykParamDTO.getRecordid());
            if(count>0)
                throw new EyasBusinessException("会员已经预约了该课程,请不要重复预约");

            //同一个时间段的课程不能预约多个
            count = commonJdbcUtils.queryObject("select count(1) from appointmentinfo a,recordinfo b where a.cid=? and a.recordid=b.recordid " +
                            " and a.status<>'3' and b.status<>'4' and b.starttime=? and (( " +
                            " (b.begintime>? and b.begintime<?) " +
                            " or (b.endtime>? and b.endtime<?) " +
                            " or (b.begintime<? and b.endtime>?) ) or b.begintime=? and b.endtime=?) ",Integer.class,ykParamDTO.getChildrens()[i],
                    simpleDateFormat1.format(recordInfo.getStarttime()),
                    simpleDateFormat2.format(recordInfo.getBegintime()),simpleDateFormat2.format(recordInfo.getEndtime()),
                    simpleDateFormat2.format(recordInfo.getBegintime()),simpleDateFormat2.format(recordInfo.getEndtime()),
                    simpleDateFormat2.format(recordInfo.getBegintime()),simpleDateFormat2.format(recordInfo.getEndtime()),
                    simpleDateFormat2.format(recordInfo.getBegintime()),simpleDateFormat2.format(recordInfo.getEndtime()));
            if(count>0)
                throw new EyasBusinessException("在["+simpleDateFormat1.format(recordInfo.getStarttime())+"  "+simpleDateFormat2.format(recordInfo.getBegintime())
                        +"]已经存在有效的预约课程");
        }

        //校验该课程是否已经开课，超过begintime的不能预约
        Date starttime = recordInfo.getStarttime();
        Date begintime = recordInfo.getBegintime();
        SimpleDateFormat dayformat = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat timeformat = new SimpleDateFormat("HHmmss");
        if(Integer.parseInt(dayformat.format(starttime))<Integer.parseInt(dayformat.format(current))) {
            throw new EyasBusinessException("该课程已经开始上课，不能预约");
        }else if(Integer.parseInt(dayformat.format(starttime))==Integer.parseInt(dayformat.format(current))) {
            if(Integer.parseInt(timeformat.format(begintime))<Integer.parseInt(timeformat.format(current)))
                throw new EyasBusinessException("该课程已经开始上课，不能预约");
        }

        try {
            app_lock.lock();
            if(!recordInfo.getStatus().equals("1"))
                throw new EyasBusinessException("这个班级的状态不是[报名中],无法预约");
            int currentnum = recordInfo.getCurrnum()==null?0:recordInfo.getCurrnum();
            int maxnum = recordInfo.getMaxnum()==null?0:recordInfo.getMaxnum();
            if(currentnum+ykParamDTO.getChildrens().length>maxnum)
                throw new EyasBusinessException("这个班级的剩余上课名额不足，不能再预约");
            recordInfo.setCurrnum(currentnum+1);
            commonJdbcUtils.update(recordInfo);
        }finally {
            app_lock.unlock();
        }

        for(int i=0;i<ykParamDTO.getChildrens().length;i++) {
            AppointmentInfo appointmentInfo = new AppointmentInfo();
            appointmentInfo.setUserid(ykParamDTO.getUserid());
            appointmentInfo.setRecordid(ykParamDTO.getRecordid());
            appointmentInfo.setCid(ykParamDTO.getChildrens()[i]);
            appointmentInfo.setAtime(new Date());
            appointmentInfo.setIsdy("1"); //APP约课
            //appointmentInfo.setAppuserid(null);//本人约课，可以不写
            appointmentInfo.setStatus("1");//未签到
            commonJdbcUtils.insert(appointmentInfo);
        }

    }

    @Override
    public List<QueryResultDTO> queryAppointmentInfoPublic(Integer userid) {
        StringBuffer sb = new StringBuffer("");
        sb.append(" select a.*,e.username as operationname,c.classid,d.coachid,b.starttime,b.begintime,b.endtime,b.address,g.cname, ")
                .append(" c.classname,d.coachname,(select keyworlds from keyworldsinfo where dcode='ISDY' and keycode=a.isdy ) as isdy_str, ")
                .append(" (case when isdy='1' then '会员本人' else (select username from admininfo where adminnid=a.appuserid) end) as appusername, ")
                .append(" (select keyworlds from keyworldsinfo where dcode='YKSTATUS' and keycode=a.status ) as status_str ")
                .append(" from appointmentinfo a left join admininfo e on a.operationid=e.adminnid ")
                .append(" ,recordinfo b,classinfo c,coachinfo d,recordandcoach f,childreninfo g ")
                .append(" where a.recordid=b.recordid and b.classid=c.classid and b.recordid=f.recordid and f.coachid=d.coachid ")
                .append(" and f.coachflag='1' and a.cid=g.cid ")
                .append(" and a.userid=? order by atime desc ");
       /* sb.append(" select a.*,e.username as operationname,c.classid,d.coachid,b.starttime,b.begintime,b.endtime,b.address, ")
                .append(" c.classname,d.coachname,(select keyworlds from keyworldsinfo where dcode='ISDY' and keycode=a.isdy ) as isdy_str, ")
                .append(" (case when isdy='1' then '会员本人' else (select username from admininfo where adminnid=a.appuserid) end) as appusername, ")
                .append(" (select keyworlds from keyworldsinfo where dcode='YKSTATUS' and keycode=a.status ) as status_str ")
                .append(" from appointmentinfo a left join admininfo e on a.operationid=e.adminnid ")
                .append(" ,recordinfo b,classinfo c,coachinfo d ")
                .append(" where a.recordid=b.recordid and b.classid=c.classid and b.coachid=d.coachid ")
                .append(" and a.userid=? order by atime desc ");*/
        return commonJdbcUtils.queryList(sb.toString(),QueryResultDTO.class,userid);
    }

    @Override
    public PageResult<QueryResultDTO> queryAppointmentStatus(QueryParamDTO dto, int page, int limit) {
        String str = "";
        String status = "";
        StringBuffer sb = new StringBuffer("");
        if(dto.getStatus().equals("4")) {
            str = " and ifnull(a.consumes,0)>0 ";
            status = "2";
        }else {
            str = " and ifnull(a.consumes,0)=0 ";
            status = dto.getStatus();
        }
        sb.append(" select a.appointmentid,a.userid,b.username,b.telephone,a.status,a.recordid,d.cname,a.cid, ")
                .append(" case when ?='1' then '未签到' when ?='2' then '已签到' else '已销课' end as statustext, ")
                .append(" a.consumes,a.consumest,a.operationid,c.username as operationname,a.operationtime ")
                .append(" from appointmentinfo a ")
                .append(" left join admininfo c on a.operationid=c.adminnid ")
                .append(" ,userinfo b,childreninfo d where a.status<>'3' and a.recordid=? and a.userid=b.userid and a.status=? and a.cid=d.cid ")
                .append(str);

        return commonJdbcUtils.queryForPage(sb.toString(),QueryResultDTO.class,page,limit,dto.getStatus(),dto.getStatus(),dto.getRecordid(),status);
    }

    @Override
    public void updateAppointmentInfoPublic(AppointMentInfoDTO paramdto) {
        if (paramdto.getAppointmentid() == null || paramdto.getAppointmentid() == 0)
            throw new EyasBusinessException("请选择要操作的约课记录");
        if (paramdto.getUserid() == null || paramdto.getUserid() == 0)
            throw new EyasBusinessException("操作人不合法，请重新登录");
        AppointmentInfo appointmentInfo = commonJdbcUtils.queryObject("select * from appointmentinfo where appointmentid=?", AppointmentInfo.class, paramdto.getAppointmentid());
        if (appointmentInfo == null)
            throw new EyasBusinessException("约课记录找不到，请刷新重试");
        if (appointmentInfo.getStatus().equals("3"))
            throw new EyasBusinessException("该约课记录已经取消，请不要重复操作");
        if(!appointmentInfo.getStatus().equals("1"))
            throw new EyasBusinessException("该约课记录已经签到，不能取消");
        if(appointmentInfo.getUserid()!=paramdto.getUserid())
            throw new EyasBusinessException("请确认这次取消约课是本人操作");

        appointmentInfo.setStatus("3");
        appointmentInfo.setConsumes(0);
        appointmentInfo.setOperationid(-1);
        appointmentInfo.setOperationtime(new Date());

        //recordinfo的当前报名人数-1
        commonJdbcUtils.update("update recordinfo set currnum=currnum-1 where recordid=?", appointmentInfo.getRecordid());

        commonJdbcUtils.update(appointmentInfo);
    }
}
